using SS;
using System;
using System.Data;
using System.Data.SqlClient;
namespace TVSBH.core
{
	internal class Partner
	{
		private Func objFunc = new Func();
		public Partner()
		{
			Program.strConnectionString = this.objFunc.strConnectionString;
		}
		public void getFistBalanceProviderUpdate(string CusID, string Balancer)
		{
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = string.Concat(new string[]
			{
				"UPDATE [TabPartner] SET [firstBalance] = ",
				Balancer,
				" WHERE [CusID] = '",
				CusID,
				"'"
			});
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			sqlCommand.ExecuteNonQuery();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public void getFistBalanceCustomerUpdate(string cID, string Balancer)
		{
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = string.Concat(new string[]
			{
				"UPDATE [TabPartner] SET [firstBalance] = ",
				double.Parse(Balancer).ToString(),
				" WHERE [CusID] = '",
				cID,
				"'"
			});
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			sqlCommand.ExecuteNonQuery();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public double getFistBalanceProvider(string pID)
		{
			double result = 0.0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT [firstBalance] FROM [TabPartner] WHERE [CusID] = '" + pID + "'";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					result = double.Parse(sqlDataReader["firstBalance"].ToString());
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public double getDiscountProvider(string CusID)
		{
			double result = 0.0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT [DiscountIn] FROM [TabGroupCustomer] WHERE [GCustomer_ID] IN (SELECT [CusGroup] FROM [TabPartner] WHERE [CusID] = '" + CusID + "')";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					result = double.Parse(sqlDataReader["DiscountIn"].ToString());
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public double getDiscountCustomer(string cID)
		{
			double result = 0.0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT [DiscountOut] FROM [TabGroupCustomer] WHERE [GCustomer_ID] IN (SELECT [CusGroup] FROM [TabPartner] WHERE [CusID] = '" + cID + "')";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					result = double.Parse(sqlDataReader["DiscountOut"].ToString());
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public double getFistBalanceCustomer(string CusID)
		{
			double result = 0.0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT [firstBalance] FROM [TabPartner] WHERE [CusID] = '" + CusID + "'";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					result = double.Parse(sqlDataReader["firstBalance"].ToString());
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public double getTotalMoney(string CusID, DateTime pointDate)
		{
			double result = 0.0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			SqlParameter sqlParameter = new SqlParameter("objDate", SqlDbType.DateTime);
			sqlParameter.Value = pointDate;
			string cmdText = "SELECT SUM([MoneyAfterDiscount]) AS MySum FROM [TabBill] WHERE [PID] = '" + CusID + "' AND [CreateDate] <= @objDate";
			SqlDataReader sqlDataReader = new SqlCommand(cmdText, sqlConnection)
			{
				Parameters = 
				{
					sqlParameter
				}
			}.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					try
					{
						result = double.Parse(sqlDataReader["MySum"].ToString());
					}
					catch
					{
						result = 0.0;
					}
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public double getTotalMoney(string CusID)
		{
			double result = 0.0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT SUM([MoneyAfterDiscount]) AS MySum FROM [TabBill] WHERE [PID] = '" + CusID + "' AND [TypeInput] IN ('OUT1','OUT2','OUT3')";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					try
					{
						result = double.Parse(sqlDataReader["MySum"].ToString());
					}
					catch
					{
						result = 0.0;
					}
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public double getTotalMoneyPayedCustomer(string pID)
		{
			double result = 0.0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT SUM([TotalMoney]) AS MySum FROM [TabInOutBill] WHERE [ObjectID] = '" + pID + "' AND [TypeInput] IN ('IN')";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					try
					{
						result = double.Parse(sqlDataReader["MySum"].ToString());
					}
					catch
					{
						result = 0.0;
					}
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public double getTotalMoneyPayedCustomer(string pID, DateTime pointDate)
		{
			double result = 0.0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			SqlParameter sqlParameter = new SqlParameter("objDate", SqlDbType.DateTime);
			sqlParameter.Value = pointDate;
			string cmdText = "SELECT SUM([TotalMoney]) AS MySum FROM [TabInOutBill] WHERE [ObjectID] = '" + pID + "' AND [TypeInput] IN ('IN')  AND [CreateDate] <= @objDate";
			SqlDataReader sqlDataReader = new SqlCommand(cmdText, sqlConnection)
			{
				Parameters = 
				{
					sqlParameter
				}
			}.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					try
					{
						result = double.Parse(sqlDataReader["MySum"].ToString());
					}
					catch
					{
						result = 0.0;
					}
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public double getTotalMoneyPayedProvider(string pID)
		{
			double result = 0.0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT SUM([TotalMoney]) AS MySum FROM [TabInOutBill] WHERE [ObjectID] = '" + pID + "' AND [TypeInput] IN ('OUT')";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					try
					{
						result = double.Parse(sqlDataReader["MySum"].ToString());
					}
					catch
					{
						result = 0.0;
					}
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public double getTotalMoneyPayedProvider(string pID, DateTime pointDate)
		{
			double result = 0.0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			SqlParameter sqlParameter = new SqlParameter("objDate", SqlDbType.DateTime);
			sqlParameter.Value = pointDate;
			string cmdText = "SELECT SUM([TotalMoney]) AS MySum FROM [TabInOutBill] WHERE [ObjectID] = '" + pID + "' AND [TypeInput] IN ('OUT') AND [TypeInput] IN ('IN') AND [CreateDate] <= @objDate";
			SqlDataReader sqlDataReader = new SqlCommand(cmdText, sqlConnection)
			{
				Parameters = 
				{
					sqlParameter
				}
			}.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					try
					{
						result = double.Parse(sqlDataReader["MySum"].ToString());
					}
					catch
					{
						result = 0.0;
					}
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public double getTotalMoneyProvider(string pID)
		{
			double num = 0.0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT SUM([MoneyAfterDiscount]) AS MySum FROM [TabBill] WHERE [PID] = '" + pID + "' AND [TypeInput] IN ('IN1','IN2','IN3')";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					try
					{
						num = double.Parse(sqlDataReader["MySum"].ToString());
					}
					catch
					{
						num = 0.0;
					}
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return Math.Abs(num - this.getTotalMoneyProviderReturn(pID));
		}
		public double getTotalMoneyProviderReturn(string pID)
		{
			double result = 0.0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT SUM([totalMoney]) AS MySum FROM [TabBill] WHERE [PID] = '" + pID.Remove(pID.Length - 1, 1) + "' AND [TypeInput] IN ('IN3')";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					try
					{
						result = double.Parse(sqlDataReader["MySum"].ToString());
					}
					catch
					{
						result = 0.0;
					}
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public string getAddress(string CusID)
		{
			string result = "";
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT [Address] FROM [TabPartner] WHERE [CusID] = '" + CusID + "'";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					result = sqlDataReader["Address"].ToString();
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public string getPhone(string pID)
		{
			string result = "";
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT [Phone] FROM [TabPartner] WHERE [CusID] = '" + pID + "'";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					result = sqlDataReader["Phone"].ToString().Replace("\"", "");
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public bool allowOutProduct(string CusID)
		{
			bool result = false;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT * FROM [TabPartner] WHERE [CusID] = '" + CusID + "'";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					result = true;
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public bool allowInProduct(string CusID)
		{
			bool result = true;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT * FROM [TabPartner] WHERE [CusID] = '" + CusID + "'";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					result = false;
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public string getAddress1(string cID)
		{
			string result = "";
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT [Address] FROM [TabPartner] WHERE [CusID] = '" + cID + "'";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					result = sqlDataReader["Address"].ToString();
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public string getPhone1(string cID)
		{
			string result = "";
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT [Phone] FROM [TabPartner] WHERE [CusID] = '" + cID + "'";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					result = sqlDataReader["Phone"].ToString().Replace("\"", "");
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public int getCountToEqualDebt()
		{
			int result = 0;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				string cmdText = "SELECT COUNT(*) AS CountPartner FROM [TabPartner]";
				SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				while (sqlDataReader.Read())
				{
					result = int.Parse(sqlDataReader["CountPartner"].ToString());
				}
				sqlDataReader.Close();
				sqlDataReader.Dispose();
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch
			{
			}
			return result;
		}
	}
}
